<?php
/**
 * Description of DepartmentMapper
 *
 /*
 * @author     Thibaut Cromphaut and Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Safti-Kado
 */
class Application_Model_ShelfMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Shelfs';
    }

    /**
     * Gebruiker met een bepaalde id ophalen
     * 
     * @param integer $id
     * @return Application_Model_Department
     */
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('shelf_id = ?' => $id));

        return $row; //$this->_toDepartment($row);
    }
    
    public function fetchByName($label)
    {
        $row = $this->getDbTable()->fetchRow(array('shelf_label = ?' => $label));

        return $row; //$this->_toDepartment($row);
    }

    /**
     * Alle Users ophalen
     * 
     * @return array
     */
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();        
        return $this->_toShelfs($rows);
        //return $this->getDbTable();
    }

    /**
     * User opslaan
     *
     * @param Application_Model_Department $department
     */
    public function save(Application_Model_Shelf $shelf)
    {
        $data = array(
            'shelf_label'             => $shelf->Label,
            'shelf_x'                 => $shelf->X,
            'shelf_y'                 => $shelf->Y,
            'shelf_h'                 => $shelf->H,
            'shelf_w'                 => $shelf->W,
            'shelf_alpha'             => $shelf->Alpha,
        );

        if (null === $shelf->Id) {
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('shelf_id = ?' => $shelf->Id));
        }
    }
    
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_Department 
     */
    private function _toShelf(Zend_Db_Table_Row_Abstract $row = null)
    {
       $shelf = new Application_Model_Shelf();

        if ($row) {
            $shelf->Id                              = $row['shelf_id'];
            $shelf->Label                           = $row['shelf_label'];
            $shelf->X                               = $row['shelf_x'];
            $shelf->Y                               = $row['shelf_y'];
            $shelf->H                               = $row['shelf_h'];
            $shelf->W                               = $row['shelf_w'];
            $shelf->Alpha                           = $row['shelf_alpha'];
            $shelf->Department_department_id        = $row['department_department_id'];
        }
        
        return $shelf;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _toShelfs(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $shelfs = array();

        if ($rows) {
            foreach ($rows as $row) {
                $shelfs[] = $this->_toShelf($row);
            }
        }
        
        return $shelfs;
    }

}